package com.ibeeking.found.uaa.cp.controller.v1;

import com.ibeeking.found.common.config.secret.RsaConfig;
import com.ibeeking.nematos.utils.result.ResponseResult;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

/**
 * @Author ibeeking
 */
@Controller
public class ClientLoginController {

    @Resource
    private RsaConfig rsaConfig;

    @GetMapping("/login")
    public ResponseEntity<ResponseResult<String>> login() {
        return ResponseEntity
                .status(HttpStatus.UNAUTHORIZED)
                .body(ResponseResult.fail(HttpStatus.UNAUTHORIZED.value(), "用户名/密码错误"));
    }

    @GetMapping("/oauth/public_key")
    @ResponseBody
    public Map<String, Object> publicKey() {
        RSAKey key = new RSAKey.Builder((RSAPublicKey) rsaConfig.getKeyPair().getPublic()).build();
        return new JWKSet(key).toJSONObject();
    }

}